﻿
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoStoreOrderDetail
    {
        public string className = "daoStoreOrderDetail";
        public daoStoreOrderDetail()
        {

        }
        private StoreOrderDetail Load(IDataReader de)
        {
            StoreOrderDetail item = new StoreOrderDetail();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.OrderId = clsConvert.DbToInt("OrderId", de);
            item.OrderDetailId = clsConvert.DbToInt("OrderDetailId", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.DesignCode = clsConvert.DbToString("DesignCode", de);
            item.Quantity = clsConvert.DbToInt("Quantity", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.StartDate = clsConvert.DbToDateTime("StartDate", de);
            item.EndDate = clsConvert.DbToDateTime("EndDate", de);
            item.ModifiedBy = clsConvert.DbToString("ModifiedBy", de);
            item.ModifiedDate = clsConvert.DbToDateTime("ModifiedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }
        public StoreOrderDetail GetByID(int orderDetailId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            StoreOrderDetail item = new StoreOrderDetail();

            try
            {
                para[0] = new SqlParameter(busStoreOrderDetail.OrderDetailId, SqlDbType.Int);
                para[0].Value = orderDetailId;
                using (de = clsSQL.SPDataReader("StoreOrderDetail_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog(className, "GetByID(int orderDetailId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }


        public StoreOrderDetailCollection List(StoreOrderDetail objOrderDetail)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            StoreOrderDetailCollection items = new StoreOrderDetailCollection();

            try
            {
                para[0] = new SqlParameter(busStoreOrderDetail.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objOrderDetail.OrderBy;
                para[1] = new SqlParameter(busStoreOrderDetail.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objOrderDetail.OrderDirection;
                para[2] = new SqlParameter(busStoreOrderDetail.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objOrderDetail.CurrentPage;
                para[3] = new SqlParameter(busStoreOrderDetail.PAGESIZE, SqlDbType.Int);
                para[3].Value = objOrderDetail.PageSize;
                para[4] = new SqlParameter(busStoreOrderDetail.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objOrderDetail.Condition;
                para[5] = new SqlParameter(busStoreOrderDetail.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("StoreDetailOrder_List", para);
                while (de.Read())
                {
                    StoreOrderDetail item = Load(de);
                    items.Add(item);
                }

                de.Close();
                objOrderDetail.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoStoreOrderDetail", "List(StoreOrderDetail objOrderDetail)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        public StoreOrderDetailCollection ListByOrder(int orderId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            StoreOrderDetailCollection items = new StoreOrderDetailCollection();

            try
            {
                para[0] = new SqlParameter(busStoreOrderDetail.OrderId, SqlDbType.Int);
                para[0].Value = orderId;
                de = clsSQL.SPDataReader("StoreOrderDetail_ListByOrderId", para);
                while (de.Read())
                {
                    StoreOrderDetail item = Load(de);
                    items.Add(item);
                }

                de.Close();              
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoStoreOrderDetail", "ListByOrder(int orderId)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
        public bool Delete(int orderDetailId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busStoreOrderDetail.OrderDetailId, SqlDbType.Int);
                para[0].Value = orderDetailId;
                result = clsSQL.SPExecute("StoreOrderDetail_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog(className, "Delete(int orderDetailId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public int Insert(StoreOrderDetail objOrderDetail)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[8];
            try
            {
                para[0] = new SqlParameter(busStoreOrderDetail.OrderId, SqlDbType.Int);
                para[0].Value = objOrderDetail.OrderId;

                para[1] = new SqlParameter(busStoreOrderDetail.Name, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objOrderDetail.Name);

                para[2] = new SqlParameter(busStoreOrderDetail.DesignCode, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objOrderDetail.DesignCode);


                para[3] = new SqlParameter(busStoreOrderDetail.Quantity, SqlDbType.Int);
                para[3].Value = clsConvert.IntToDB(objOrderDetail.Quantity);

                para[4] = new SqlParameter(busStoreOrderDetail.CreatedDate, SqlDbType.DateTime);
                para[4].Value = objOrderDetail.CreatedDate;

                para[5] = new SqlParameter(busStoreOrderDetail.CreatedBy, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objOrderDetail.CreatedBy);

                para[6] = new SqlParameter(busStoreOrderDetail.EndDate, SqlDbType.DateTime);
                para[6].Value = clsConvert.DateToDB(objOrderDetail.EndDate);
                para[7] = new SqlParameter(busStoreOrderDetail.StartDate, SqlDbType.DateTime);
                para[7].Value = clsConvert.DateToDB(objOrderDetail.StartDate);

                result = clsSQL.SPExecute("StoreOrderDetail_Insert", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog(className, "Insert(StoreOrderDetail objOrderDetail)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public int Update(StoreOrderDetail objOrderDetail)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[8];
            try
            {
                //para[0] = new SqlParameter(busStoreOrderDetail.OrderId, SqlDbType.Int);
                //para[0].Value = objOrderDetail.OrderId;

                para[0] = new SqlParameter(busStoreOrderDetail.Name, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objOrderDetail.Name);

                para[1] = new SqlParameter(busStoreOrderDetail.DesignCode, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objOrderDetail.DesignCode);


                para[2] = new SqlParameter(busStoreOrderDetail.Quantity, SqlDbType.Int);
                para[2].Value = clsConvert.IntToDB(objOrderDetail.Quantity);

                para[3] = new SqlParameter(busStoreOrderDetail.ModifiedDate, SqlDbType.DateTime);
                para[3].Value = objOrderDetail.ModifiedDate;

                para[4] = new SqlParameter(busStoreOrderDetail.ModifiedBy, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objOrderDetail.ModifiedBy);

                para[5] = new SqlParameter(busStoreOrderDetail.StartDate, SqlDbType.DateTime);
                para[5].Value = clsConvert.DateToDB(objOrderDetail.StartDate);
                para[6] = new SqlParameter(busStoreOrderDetail.EndDate, SqlDbType.DateTime);
                para[6].Value = clsConvert.DateToDB(objOrderDetail.EndDate);
                para[7] = new SqlParameter(busStoreOrderDetail.OrderDetailId, SqlDbType.Int);
                para[7].Value = objOrderDetail.OrderDetailId;
                result = clsSQL.SPExecute("StoreOrderDetail_Update", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog(className, "Update(StoreOrderDetail objOrderDetail)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
    }
}
